package com.fdb.efp.esb.service.impl.tranditionalcore;

import com.alibaba.fastjson.JSONObject;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.constant.ESBClientConstance;
import com.fdb.efp.esb.common.constant.EsbBizEnums;
import com.fdb.efp.esb.service.facade.tranditionalcore.AccountQueryService;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.efp.esb.service.bo.req.traditionalcore.AccountQueryReqBean;
import com.fdb.efp.esb.service.bo.resp.traditionalcore.AccountQueryRespBean;
import com.fdb.efp.esb.service.client.ESBRequestBuilder;
import com.fdb.efp.esb.service.client.EsbBaseBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("accountQueryService")
public class AccountQueryServiceImpl implements AccountQueryService {
    private static Logger logger = LoggerFactory.getLogger(AccountQueryServiceImpl.class);

    @Override
    public AccountQueryRespBean applyAccountQuery(AccountQueryReqBean accountQueryReqBean) throws ESBException {
        logger.info("======>调用账户基本信息查询接口【" + accountQueryReqBean.getGlobaNo() + "_30130001_01】开始<======");
        AccountQueryRespBean accountQueryRespBean = null;
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean()
					.withINSTID(ESBClientConstance.bdInstId)
					.withGLBLSRLNO(accountQueryReqBean.getGlobaNo()).build();
            EsbRespServiceBean recvBean = ESBRequestBuilder
					.buildClient()
					.withEsbBaseBean(esbBaseBean)
                    .withBody(accountQueryReqBean)
                    .withTradeNo(ESBConstant.TRADE_NO_30130001)
                    .withScene(ESBConstant.SCENE_NO_01)
                    .create()
                    .sendAndReceive();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                accountQueryRespBean = recvBean.getBody(AccountQueryRespBean.class);
            } else {
                throw new ESBException(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            }
            logger.info("调用账户基本信息查询接口【30130001_01】结束，返回信息：" + JSONObject.toJSONString(accountQueryRespBean));
        } catch (Exception e) {
            logger.error("调用账户基本信息查询接口【30130001_01】出现异常：" + e.getMessage());
            throw new ESBException("调用账户基本信息查询接口【30130001_01】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>调用账户基本信息查询接口【" + accountQueryReqBean.getGlobaNo() + "_30130001_01】结束<======");
        }
        return accountQueryRespBean;
    }
}